package com.sg.hms.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.sg.hms.Model.Applicationer;
import com.sg.hms.Model.Department2;
import com.sg.hms.Model.Employee2;
import com.sg.hms.Service.MoneyApplicationService;
import com.sg.hms.Service.SalaryService;
import com.sg.hms.ServiceImpl.MoneyApplicationServiceImpl;
import com.sg.hms.ServiceImpl.SalaryServiceImpl;
/**
 * 
 * @author lubinghan
 *
 */
public class ShowTotalSpendsContraller extends HttpServlet{
	SalaryService sService = new SalaryServiceImpl();
	MoneyApplicationService mService = new MoneyApplicationServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.setCharacterEncoding("utf-8");
		req.setCharacterEncoding("utf-8");
		PrintWriter pw = resp.getWriter();
		//获取请求参数
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		int pageStart = (page-1)*pageSize;
		
		String dateStr = req.getParameter("date");
		if(dateStr==null){//加载时日期为空则设为当前日
			Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			int month = c.get(Calendar.MONTH)+1;
			int date = c.get(Calendar.DATE); 
			dateStr = year + "-" + month + "-" + date ;
		}
		//截取日期
		String datestr1 = dateStr.substring(0, 4);
		String dateStr2 = dateStr.substring(5,7);
		String date = datestr1+dateStr2;
		List<Applicationer> applicationers = mService.selectDeparTotalMoneyByDate(pageStart, pageSize, date);//部门申请后批准消费
		//将查询的部门申请后批准消费放入部门对象
		List<Department2> departments1 = new ArrayList<Department2>();
		for (Applicationer applicationer : applicationers) {
			Department2 department = new Department2();
			department.setdName(applicationer.getdName());
			department.setMoney(applicationer.getMoney());
			departments1.add(department);
		}
		
		//获得部门申请后批准消费的数据条数
		List<Employee2> employees = sService.selectDepartSalarySpendByDate(date, pageStart, pageSize);//部门员工工资和
		//将部门员工工资和放入部门对象
		List<Department2> departments2 = new ArrayList<Department2>();
		for (Employee2 employee2 : employees) {
			Department2 department = new Department2();
			department.setdName(employee2.getdName());
			department.setSalary(Integer.parseInt(employee2.getSalary()));
			departments2.add(department);
		}
		
		//合并部门申请消费和工资支出两个对象
		departments2.addAll(departments1);
		for (int i = 0; i <departments2.size()-1; i++) {
			for (int j = departments2.size()-1; j >i ; j--) {
				if (departments2.get(i).getdName().equals(departments2.get(j).getdName())) {
					departments2.get(i).setMoney(departments2.get(j).getMoney());
					departments2.remove(j);
				}
			}
		}
		//设置各部门查询的月中的总消费,部门支出总消费，部门工资总消费
		for (Department2 department : departments2) {
			department.setSpends(department.getMoney()+department.getSalary());
		}
		int total = sService.selectCountDepartSpendsByDate(date);//查询月总消费数据条数
		double[] departSpend = sService.selectDepartSpendsByDate(date);//查询一月批准申请总支出
		//总的开销
		int totalspends = sService.selectTotalSpendsByDate(date);
		List<Department2> departments = new ArrayList<Department2>();
		Department2 department = new Department2();
		department.setdName("该月支出合计");
		department.setSpends(totalspends);
		department.setMoney(departSpend[0]);
		department.setSalary((int)departSpend[1]);
		departments.add(department);
		
		JSONObject totalspendgrid = new JSONObject();//创建json对象
		totalspendgrid.put("rows", departments2);//将查询月的总消费相关信息放入josn对象
		totalspendgrid.put("total", total);
		totalspendgrid.put("footer", departments);
		pw.write(totalspendgrid.toString());
	}
}
